package com.rocksdata.mc.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.ctz.commonframe.ui.TitleBarActivity;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.entity.CourseInfo;
import com.rocksdata.mc.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * Created by 陈堂正 on 2017/5/17.
 */
@ContentView(R.layout.activity_preview_release)
public class PreviewReleaseActivity extends TitleBarActivity {
    @ViewInject(R.id.et_preview_content)
    private EditText etContent;

    private CourseInfo courseInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        courseInfo = (CourseInfo) getIntent().getSerializableExtra("course_info");
        setTitleMiddleText("发布预习");
        setBackgroundColor(getResources().getColor(R.color.light_black));
        setTitleRightText("提交");
        setTitleRightEnable(false);
        setTitleRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = etContent.getText().toString().trim();
                releasePreview(content);
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setTitleRightEnable(s.length() > 0);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void releasePreview(String content) {
        BusinessManager.getTimetableManager().releasePreview(courseInfo, content, new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                showToast(message);
                if(isSuccess)
                    finish();
            }
        });
    }
}
